let app = getApp();
Page({
  data:{
    goods_list: [],
    zh: true,//综合排序
    zx: false,//最新排序
    zr: false,//最热排序
    off:false,
    def:true,
    up:false,
    down:false,
    act:'',
    actorder:'',
    keyword:'',
    sort_text:'',
    search_show:false,
    
    show:0,
    total:'',
    firstRow:0,
    is_show:0,
    is_show_time: 1
  },
  onLoad:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = {k: key, u: uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      sort_text:'综合排序'

    })
  },
  // 搜索
  contentInput: function(re){
    // console.log(re.detail.value)
    var that=this
    if(re.detail.value){
      that.setData({
        keyword: re.detail.value,
        search_show:true
      })
    }else{
      that.setData({
        search_show: false
      })
    }
   
  },
  bindViewTap:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { act: that.data.act, actorder: that.data.actorder, keywords:that.data.keyword, k: key, u: uid };
    that.requests(e);
  
  },
  search_clear:function(){
   this.setData({
     keyword:"",
     search_show: false

   }) 
  },
  // 综合
  sort_drap: function () {
    var that=this
    that.setData({
      off:!that.data.off
    })
  },
  
  to_default:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { k: key, u: uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off: false,
      def:true,
      up:false,
      down:false,
      actorder: '',
      act: '',
      sort_text: '综合排序'
    })
  },
  // 佣金从高到底
  to_down:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { actorder: "DESC", act: "commission", k: key, u: uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off:false,
      def:false,
      up: false,
      down: true,
      actorder:'DESC',
      act:'commission',
      sort_text: '佣金从高到底'
    })
  },
  // 佣金从低到高
  to_up: function () {
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { actorder: " ASC", act: "commission", k: key, u: uid };
    that.requests(e);
    that.setData({
      zh: true,
      zx: false,
      zr: false,
      off: false,
      def: false,
      up: true,
      down: false,
      actorder: 'ASC',
      act: 'commission',
      sort_text: '佣金从低到高'
    })
  },
  //最新
  new_best: function(){
    var that=this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { actorder: " DESC", act: "uptime", k: key, u: uid,keywords:that.data.keyword };
    that.requests(e);
    that.setData({
      zh: false,
      zx: true,
      zr: false,
      actorder: 'DESC',
      act: 'uptime'
      
    })
  },
  //最热
  hot_best: function () {
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var e = { actorder: " DESC", act: "sales", k: key, u: uid,keywords: that.data.keyword};
    that.requests(e);
    that.setData({
      zh: false,
      zx: false,
      zr: true,
      actorder: 'DESC',
      act: 'sales'
    })
  },

  requests:function(e){
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Goods&met=index&typ=json',
      data: e,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      responseType: 'text',
      success: function (res) {
        console.log(res)
        that.setData({
          goods_list: res.data.data.items,
          total: res.data.data.total,
          is_show_time:0,
          firstRow: 0,
          is_show:0
        })
      }
    })

  },

  bindhref:function(){
    app.goTop()
    this.setData({
      show:0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom:function(){
    
    var that=this
    if (that.data.is_show == 1) {
       return false
     }
    that.setData({
      firstRow: that.data.firstRow + 10
    })
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Goods&met=index&typ=json',
      header: {
        'content-type': 'application/json'
      },
      data: { act: that.data.act, actorder: that.data.actorder, keywords: that.data.keyword, firstRow: that.data.firstRow,k:key,u:uid},
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res)
        var arr = that.data.goods_list
         if (res.data.data.items.length > 0){
          
           for (var z in res.data.data.items) {
             arr.push(res.data.data.items[z])
           }
          that.setData({
            goods_list:arr,
            
          })
         }else{
           that.setData({
             is_show: 1
          
           })
         }

      }
    })
  }

})